<template>
  <div>
    <el-card class="box-card">
      <el-button size="mini" class="category_btn" @click="onAddCate">添加</el-button>
      <el-table
        :data="resData"
        border
        style="width: 100%">
        <el-table-column
          prop="id"
          label="编号"
          width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          label="创建时间">
        </el-table-column>
        <el-table-column
          prop="sort"
          label="排序">
        </el-table-column>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <add-cate
      ref="handlerAddCate"
      @watchChild="refreshPage"
      :isEdit="isEdit"
      :dialogVisible="dialogVisible"
      :cate_name="currentData.name"
      :cate_sort="currentData.sort"
      :cate_id="currentData.id"/>
  </div>
</template>

<script lang='ts'>
import Vue from 'vue'
import AddCate from '@/views/resource/components/AddCate.vue'
import { CateAdd, deleteCate, getAllCategory } from '@/services/resource'
export default Vue.extend({
  name: 'ResourceCategory',
  components: {
    AddCate
  },
  data () {
    return {
      resData: [],
      dialogVisible: false,
      isEdit: false,
      currentData: {}
    }
  },
  methods: {
    // 获取所有资源分类
    async handlerAllCate () {
      const { data } = await getAllCategory()
      if (data.code === '000000') {
        this.resData = data.data
      }
    },
    // 添加分类
    onAddCate () {
      this.isEdit = false
      this.dialogVisible = true
      // (this.$refs.handlerAddCate as any).recordResourceBol(this.dialogVisible)
    },
    // 编辑分类
    async handleEdit (row:CateAdd) {
      this.isEdit = true
      this.dialogVisible = true
      this.currentData = this.resData.filter((item:any) => item.id === row.id)[0]
    },
    // 删除分类
    handleDelete (row:CateAdd) {
      // console.log(row)
      this.$confirm('此操作将删除一项分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data } = await deleteCate(row.id as number)
        if (data.code === '000000') {
          this.$message.success('删除成功')
          this.handlerAllCate()
        }
      }).catch(() => {
        this.$message.error('删除失败')
      })
    },
    // 刷新页面
    refreshPage () {
      this.dialogVisible = false
      // this.currentData = {}
      this.handlerAllCate()
    }
  },
  created () {
    // 分类列表
    this.handlerAllCate()
  }

})
</script>

<style lang="scss" scoped>
.category_btn{
  margin-bottom: 2rem;
}
</style>
